package leetcode; /**
 * description:
 * author:张腾
 * date:2021-07-23
 */

/**
 * 给定一个n个元素有序的（升序）整型数组nums 和一个目标值target ，写一个函数搜索nums中的 target，如果目标值存在返回下标，否则返回 -1。
 */
public class leetcode704 {
    public int search(int[] nums, int target) {
        int l = 0, r = nums.length-1;
        while (l<=r){
            int mid = (l+r)>>1;
            if (nums[mid]==target) return mid;
            else if (nums[mid]<target) l = mid+1;
            else if (nums[mid]>target) r = mid-1;
        }
        return -1;
    }
}
